﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;
namespace LiWi
{
    public partial class 手轮测试 : Form
    {
        IntPtr _handle { get { return Data.handle; } }
        bool isConnect { get { return Data.handle == (IntPtr)0 ? false : true; } }
        public WindowsAdapt adapt = new WindowsAdapt();
        public 手轮测试()
        {
            InitializeComponent();


            adapt.SetForm(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (isConnect) {
                PMC_SetAtype(_handle, 0, 1);
                PMC_SetAtype(_handle, 1, 1);
                PMC_SetAtype(_handle, 2, 1);
                int connectAxis = 0;
                if (radioButton_x.Checked)
                    connectAxis = 0;
                else if (radioButton_y.Checked)
                    connectAxis = 1;
                else if (radioButton_z.Checked)
                    connectAxis = 2;
                if (radioButton_ab.Checked)
                {
                    PMC_SetAtype(_handle, 3, 3);
                    //PMC_SetClutchRate(_handle, connectAxis, 3);
                }
                else if (radioButton_pluseAddDir.Checked)
                {
                    PMC_SetAtype(_handle, 3, 6);
                    //PMC_SetClutchRate(_handle, connectAxis, 3);
                }
                    PMC_Connect(_handle, (float)numericUpDown_ratio.Value, 3, connectAxis);
                label_state.Text = "手轮链接状态：已链接";

                groupBox2.Enabled = false;
                button1.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (isConnect) {
                PMC_Rapidstop(_handle, 3);
                label_state.Text = "手轮链接状态：未链接";
                groupBox2.Enabled = true;
                button1.Enabled = true;
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!isConnect)
                return;
            PMC_SetDpos(_handle, 0, 0);
            PMC_SetDpos(_handle, 1, 0);
            PMC_SetDpos(_handle, 2, 0);
            PMC_SetDpos(_handle, 3, 0);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isConnect) { 
                float[] posArray= new float[4];

                for (int i = 0; i < posArray.Length; i++) {
                    PMC_GetDpos(_handle, i, ref posArray[i]);
                }

                label_x.Text = "AXIS0:" + Convert.ToString(posArray[0]);
                label_y.Text = "AXIS1:" + Convert.ToString(posArray[1]);
                label_z.Text = "AXIS2:" + Convert.ToString(posArray[2]);
                label_e.Text = "手轮轴:" + Convert.ToString(posArray[3]);


            }
        }
    }
}
